<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>事件操作和事件对象</title>
    <style>
      button {
        cursor: pointer;
      }

      .box2 {
        margin-top: 40px;
      }
      .box2 > .father {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100px;
        height: 100px;
        background-color: pink;
        margin-top: 5px;
      }
      .box2 > .father > .son {
        width: 50px;
        height: 50px;
        background-color: skyblue;
      }
    </style>
  </head>
  <body>
    <!-- 1、注册事件的两种方式 -->
    <div class="box1">
      <button id="tradition">传统方式</button>
      <button id="method">方法监听</button>
    </div>

    <!-- 2、阻止事件默认行为、阻止事件冒泡 -->
    <div class="box2">
      <a href="https://www.baidu.com">百度</a>
      <div class="father">
        <div class="son"></div>
      </div>
    </div>

    <script>
      // 1、注册事件的两种方式
      // 1.1 传统方式
      var tradition = document.getElementById('tradition');
      tradition.onclick = function () {
        alert('传统方式');
      };
      // 1.2 方法监听注册方式（注意：前面不要加 on）
      var method = document.getElementById('method');
      method.addEventListener('click', function () {
        alert('方法监听注册方式');
      });

      // 2、阻止事件默认行为、阻止事件冒泡
      // 2.1 阻止事件默认行为
      var aEle = document.querySelector('a');
      aEle.onclick = function (e) {
        e.preventDefault();
      };
      // 2.2 阻止事件冒泡
      var father = document.querySelector('.father');
      var son = document.querySelector('.son');
      father.addEventListener('click', function () {
        alert('father');
      });
      son.addEventListener('click', function (e) {
        alert('son');
        e.stopPropagation();
      });
    </script>
  </body>
</html>
